package com.chinairi.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinairi.common.annotation.DoPage;
import com.chinairi.mall.config.ValidationResult;
import com.chinairi.mall.config.ValidatorImpl;
import com.chinairi.mall.controller.viewobject.TACVO;
import com.chinairi.mall.controller.viewobject.TCVO;
import com.chinairi.mall.error.BusinessException;
import com.chinairi.mall.error.EmBusinessError;
import com.chinairi.mall.model.TrackAlarmConfig;
import com.chinairi.mall.dao.TrackAlarmConfigMapper;
import com.chinairi.mall.model.TrackConfig;
import com.chinairi.mall.service.ITrackAlarmConfigService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 节点预警信息配置表 服务实现类
 * </p>
 *
 * @author Webgasd
 * @since 2020-08-17
 */
@Service
public class TrackAlarmConfigServiceImpl extends ServiceImpl<TrackAlarmConfigMapper, TrackAlarmConfig> implements ITrackAlarmConfigService {
    @Autowired
    private TrackAlarmConfigMapper trackAlarmConfigMapper;
    @Autowired
    private ValidatorImpl validator;

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void addPoint(TACVO tacvo) {
        ValidationResult result = validator.validate(tacvo);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(tacvo.getTrackNumber(), tacvo.getTacPre(), tacvo.getTacWay(), tacvo.getTacId())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }

        TrackAlarmConfig trackAlarmConfig = new TrackAlarmConfig();
        BeanUtils.copyProperties(tacvo,trackAlarmConfig);
        trackAlarmConfig.setTacId(UUID.randomUUID().toString().replace("-",""));
        trackAlarmConfigMapper.insert(trackAlarmConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updatePoint(TACVO tacvo) {

        ValidationResult result = validator.validate(tacvo);
        if (result.isHashErrors()){
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }

        if(checkAllExist(tacvo.getTrackNumber(), tacvo.getTacPre(), tacvo.getTacWay(), tacvo.getTacId())) {
            throw new BusinessException(EmBusinessError.ITEMS_Exist);
        }

        TrackAlarmConfig trackAlarmConfig = new TrackAlarmConfig();
        BeanUtils.copyProperties(tacvo,trackAlarmConfig);
        trackAlarmConfigMapper.updateById(trackAlarmConfig);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deletePoint(String uuid) {
        trackAlarmConfigMapper.deleteById(uuid);
    }


    @Override
    @DoPage
    public List<TrackAlarmConfig> getPoints(TACVO tacvo) {
        QueryWrapper<TrackAlarmConfig> wrapper = new QueryWrapper<>();
        wrapper
                .eq(StringUtils.isNotEmpty(tacvo.getTrackNumber()),"TRACK_NUMBER",tacvo.getTrackNumber());

        List<TrackAlarmConfig> list = trackAlarmConfigMapper.selectList(wrapper);
        return list;
    }

    public boolean checkAllExist(String number, Integer pre, String way, String id) {
        return trackAlarmConfigMapper.countByAll(number,pre,way,id) > 0;
    }
}
